package com.ocom.payconfig.controller;


import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfWalletInfo;
import com.ocom.common.request.payconfig.xfwalletinfo.GetConsAllWalletRequest;
import com.ocom.common.request.payconfig.xfwalletinfo.GetConsWalletListRequest;
import com.ocom.common.request.payconfig.xfwalletinfo.SetConsAllWalletRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.SubAccountVo;
import com.ocom.payconfig.service.XfSubAccountService;
import com.ocom.payconfig.service.XfWalletInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
public class XfWalletInfoController {


    @Autowired
    private XfWalletInfoService xfWalletInfoService;

    @Autowired
    private XfSubAccountService xfSubAccountService;

    @PostMapping(PayConfigUrl.Url_GetConsAllWallet)
    public Result getConsAllWallet(@RequestBody GetConsAllWalletRequest request){
        List<EntityXfWalletInfo> list = xfWalletInfoService.findByCompanyIdAndState(request.getComId(),null);
        Optional<EntityXfWalletInfo> optional = list.stream().max(Comparator.comparing(EntityXfWalletInfo::getWalletNo));
        if (!optional.isPresent() || 5 > optional.get().getWalletNo()){
            EntityXfWalletInfo entityXfWalletInfo = new EntityXfWalletInfo();
            entityXfWalletInfo.setComId(request.getComId());
            entityXfWalletInfo.setWalletNo((optional.isPresent() ? optional.get().getWalletNo() : 0) + 1);
            xfWalletInfoService.saveXfWalletInfo(request.getComId(),entityXfWalletInfo);
        }
        return ResultUtil.success(xfWalletInfoService.findByCompanyIdAndState(request.getComId(), null));
    }

    @PostMapping(PayConfigUrl.Url_GetConsWalletList)
    public Result getConsWalletList(@RequestBody GetConsWalletListRequest request){
        return ResultUtil.success(xfWalletInfoService.findByCompanyIdAndState(request.getComId(), 1));
    }

    @PostMapping(PayConfigUrl.Url_SetConsWallet)
    public Result setConsWallet(@RequestBody SetConsAllWalletRequest request){
        return xfWalletInfoService.setConsWallet(request.getComId(), request);
    }

    @PostMapping(PayConfigUrl.Url_getSubAccountList)
    public Result getSubAccountList(@RequestParam(value = "comId") Long comId) {
        return xfSubAccountService.getSubAccountList(comId);
    }

    @PostMapping(PayConfigUrl.Url_setSubAccount)
    public Result setSubAccount(@RequestBody SubAccountVo request) {
        return xfSubAccountService.setSubAccount(request.getComId(),request);
    }
}